﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace HTSManager
{
    [Serializable]
    class UserInfo
    {
        const string strWMPPath = @"c:\wmp";
        const string strUserInfoIniFile = @"\user\commdata.ini";
        private iniUtil _UserInfoIni;

        private PlatformType _platformType;                         
        private SettingTarketType _settingTarketType;           // 해당 플랫폼 로컬을 사용할지, 매니저 속성 사용할지 구분
        private ServerType _ServerType;                         // 고객용, 직원용 구분
        private string _ServerIP;
        private ConnectionServerType _ConnectionServer;         // 운영, 테스트 구분
        private string _UserID;
        private string _UserPassword;
        private string _UserCertPassword;
        private bool _IsMarketInquary;
        private bool _IsCheckForgery;
        private DownloadType _DownloadType;

        public string strExecuteFilePath;                        // 실행파일 위치 설정
        public string strUserInfoPath;                          // 플랫폼 별 속성 정보를 가지고 있는 path

        public PlatformType PlatformType
        {
            get
            {
                return _platformType;
            }
            set
            {
                _platformType = value;
                SetPlatformPath();
            }
        }

        public SettingTarketType SettingTarketType
        {
            get
            {
                return _settingTarketType;
            }
            set
            {
                _settingTarketType = value;
            }
        }

        public string ServerIP
        {
            get
            {
                /*
                switch (_platformType)
                {
                    case PlatformType.MUG:
                    case PlatformType.TX:
                        if (_UserInfoIni.GetIniValue("GENERAL", "SYSTEM_TYPE", "CLIENT").Equals("CLIENT"))
                        {
                            if (_UserInfoIni.GetIniValue("CLIENT", "COMM_TYPE_V2", "1").Equals("1"))
                                _ServerIP = "hts.wontrading.com";
                            else
                                _ServerIP = _UserInfoIni.GetIniValue("CLIENT", "SERVER", "");
                        }
                        else
                        {
                            if (_UserInfoIni.GetIniValue("BRANCH", "SERVER", "staff.wm.com").Equals("staff.wm.com"))
                                _ServerIP = "staff.wm.com";
                            else
                                _ServerIP = _UserInfoIni.GetIniValue("BRANCH", "SERVER", "");
                        }
                        break;
                    case PlatformType.QTRADING:
                        _ServerIP = _UserInfoIni.GetIniValue("CLIENT", "SERVER", "newqtrade.wooriwm.com");
                        break;
                    case PlatformType.FORMDE:
                        _ServerIP = _UserInfoIni.GetIniValue("COMM", "LASTES_SVR", "");
                        break;
                    default:
                        break;
                }
                 * */
                return _ServerIP;
            }
            set
            {
                /*
                switch (_platformType)
                {
                    case PlatformType.MUG:
                    case PlatformType.TX:
                        if (_ServerType == ServerType.CUSTOMER)
                        {
                            if (_ConnectionServer == ConnectionServerType.OPERATION)
                                _UserInfoIni.SetIniValue("CLIENT", "COMM_TYPE_V2", "1");
                            else
                            {
                                _UserInfoIni.SetIniValue("CLIENT", "COMM_TYPE_V2", "2");
                                _UserInfoIni.SetIniValue("CLIENT", "SERVER", _ServerIP.ToString());
                            }
                        }
                        else
                            _UserInfoIni.SetIniValue("BRANCH", "SERVER", _ServerIP.ToString());
                        break;
                    case PlatformType.QTRADING:
                        _UserInfoIni.GetIniValue("CLIENT", "SERVER", _ServerIP.ToString());
                        break;
                    case PlatformType.FORMDE:
                        _UserInfoIni.GetIniValue("COMM", "LASTES_SVR", _ServerIP.ToString());
                        break;
                    default:
                        break;
                }
                 * */
                _ServerIP = value;
            }
        }

        public string UserID
        {
            get
            {
                /*
                switch (_platformType)
                {
                    case PlatformType.MUG:
                    case PlatformType.TX:
                        if (_UserInfoIni.GetIniValue("GENERAL", "SYSTEM_TYPE", "CLIENT").Equals("CLIENT"))
                            _UserID = _UserInfoIni.GetIniValue("CLIENT", "USERID", "");
                        else
                            _UserID = _UserInfoIni.GetIniValue("BRANCH", "USERID", "");
                        break;
                    case PlatformType.QTRADING:
                        _UserID = _UserInfoIni.GetIniValue("CLIENT", "USERID", "");
                        break;
                    case PlatformType.FORMDE:
                        _UserID = _UserInfoIni.GetIniValue("COMM", "ID", "");
                        break;
                    default:
                        break;
                }
                 * */
                return _UserID;
            }
            set
            {
                _UserID = value;
            }
        }

        public string UserCertPassword
        {
            get
            {
                return _UserCertPassword;
            }
            set
            {
                _UserCertPassword = value;
            }
        }

        public string UserPassword
        {
            get
            {
                return _UserPassword;
            }
            set
            {
                _UserPassword = value;
            }
        }

        public bool IsMarketInquary
        {
            get
            {
                /*
                switch (_platformType)
                {
                    case PlatformType.MUG:
                    case PlatformType.TX:
                        if (_UserInfoIni.GetIniValue("GENERAL", "SYSTEM_TYPE", "CLIENT").Equals("CLIENT"))
                            _IsMarketInquary = _UserInfoIni.GetIniValueToBoolean("INQUIRY", "IS_CHECK");
                        break;
                    case PlatformType.QTRADING:
                        _IsMarketInquary = _UserInfoIni.GetIniValueToBoolean("INQUIRY", "IS_CHECK");
                        break;
                    case PlatformType.FORMDE:
                        break;
                    default:
                        break;
                }
                */
                return _IsMarketInquary;
            }
            set
            {
                _IsMarketInquary = value;
            }
        }

        public bool IsCheckForgery
        {
            get
            {
                if (_settingTarketType == SettingTarketType.PLATFORM)
                    _IsCheckForgery = _UserInfoIni.GetIniValueToBoolean("VERSION", "CHECK");
                return _IsCheckForgery;
            }
            set
            {
                _UserInfoIni.SetIniValueToBoolean("VERSION", "CHECK", _IsCheckForgery);
                _IsCheckForgery = value;
            }
        }

        public ServerType ServerType
        {
            get
            {
                /*
                switch (_platformType)
                {
                    case PlatformType.MUG:
                    case PlatformType.TX:
                        if (_UserInfoIni.GetIniValue("GENERAL", "SYSTEM_TYPE", "CLIENT").Equals("CLIENT"))
                            _ServerType = ServerType.CUSTOMER;
                        else
                            _ServerType = ServerType.EMPOLYEE;
                        break;
                    case PlatformType.QTRADING:
                        _ServerType = ServerType.CUSTOMER;
                        break;
                    case PlatformType.FORMDE:
                        _ServerType = ServerType.EMPOLYEE;
                        break;
                    default:
                        break;
                }
                */
                return _ServerType;
            }
            set
            {
                _ServerType = value;
                //if (_settingTarketType == SettingTarketType.PLATFORM)
                //{
                    switch (_platformType)
                    {
                        case PlatformType.MUG:
                            if (_ServerType == ServerType.CUSTOMER)
                                _UserInfoIni.SetIniValue("GENERAL", "SYSTEM_TYPE", "CLIENT");
                            else
                                _UserInfoIni.SetIniValue("GENERAL", "SYSTEM_TYPE", "BRANCH");
                            break;
                        default:
                            break;
                    }
                //}
            }
        }

        public ConnectionServerType ConnectionServer
        {
            get
            {
                /*
                switch (_platformType)
                {
                    case PlatformType.MUG:
                    case PlatformType.TX:
                        if (_UserInfoIni.GetIniValue("GENERAL", "SYSTEM_TYPE", "CLIENT").Equals("CLIENT"))
                        {
                            if (_UserInfoIni.GetIniValue("CLIENT", "COMM_TYPE_V2", "1").Equals("1"))
                                _ConnectionServer = ConnectionServerType.OPERATION;
                            else
                                _ConnectionServer = ConnectionServerType.TEST;
                        }
                        else
                        {
                            if (_UserInfoIni.GetIniValue("BRANCH", "SERVER", "staff.wm.com").Equals("staff.wm.com"))
                                _ConnectionServer = ConnectionServerType.OPERATION;
                            else
                                _ConnectionServer = ConnectionServerType.TEST;
                        }
                        break;
                    case PlatformType.QTRADING:
                        if (_UserInfoIni.GetIniValue("CLIENT", "SERVER", "newqtrade.wooriwm.com").Equals("newqtrade.wooriwm.com"))
                            _ConnectionServer = ConnectionServerType.OPERATION;
                        else
	                        _ConnectionServer = ConnectionServerType.TEST;
                        break;
                    case PlatformType.FORMDE:
                        _ConnectionServer = ConnectionServerType.TEST;
                        break;
                    default:
                        break;
                }
                */
                return _ConnectionServer;
            }
            set
            {
                _ConnectionServer = value;
                //if (_settingTarketType == SettingTarketType.PLATFORM)
                //{
                    switch (_platformType)
                    {
                        case PlatformType.MUG:
                        case PlatformType.TX:
                            if (_ServerType == ServerType.CUSTOMER)
                            {
                                if (_ConnectionServer == ConnectionServerType.OPERATION)
                                    _UserInfoIni.SetIniValue("CLIENT", "COMM_TYPE_V2", "1");
                                else
                                    _UserInfoIni.SetIniValue("CLIENT", "COMM_TYPE_V2", "2");
                            }
                            break;
                        default:
                            break;
                    }
                //}
            }
        }

        public DownloadType DownloadType
        {
            get
            {
                if (_settingTarketType == SettingTarketType.PLATFORM)
                {
                    string a = _UserInfoIni.GetIniValue("VERSION", "DOWNLOAD", "0");
                    _DownloadType = (DownloadType)Enum.Parse(typeof(DownloadType), a);
                }
                return _DownloadType;
            }
            set
            {
                _DownloadType = value;
                //if (_settingTarketType == SettingTarketType.PLATFORM)
                //{
                    int nDownloadType = (int)_DownloadType;
                    _UserInfoIni.SetIniValue("VERSION", "DOWNLOAD", nDownloadType.ToString());
                //}

            }
        }

        private void SetPlatformPath()
        {
            switch (_platformType)
            {
                case PlatformType.MUG:
                    strUserInfoPath = strWMPPath + @"\mug" + strUserInfoIniFile;
                    strExecuteFilePath = strWMPPath + @"\mug" + @"\wmtslogin.exe";
                    break;
                case PlatformType.TX:
                    strUserInfoPath = strWMPPath + @"\tx" + strUserInfoIniFile;
                    strExecuteFilePath = strWMPPath + @"\tx" + @"\txlogin.exe";
                    break;
                case PlatformType.QTRADING:
                    strUserInfoPath = strWMPPath + @"\qbin" + strUserInfoIniFile;
                    strExecuteFilePath = strWMPPath + @"\qbin" + @"\QTradingLogIn.exe";
                    break;
                case PlatformType.FORMDE:
                    strUserInfoPath = strWMPPath + @"\formde" + @"\user\envdata.ini";
                    strExecuteFilePath = strWMPPath + @"\formde" + @"\FormDe.exe";
                    break;
                default:
                    break;
            }

            _UserInfoIni = new iniUtil(strUserInfoPath);
        }

        public UserInfo(PlatformType platformType, SettingTarketType settingTarketType)
        {
            _platformType = platformType;
            _settingTarketType = settingTarketType;
            /*
            if (_settingTarketType == SettingTarketType.MANAGER)
            {
                // 향후 Method 형태로 변경.
                FileInfo exefileinfo = new FileInfo(Application.ExecutablePath);
                string path = exefileinfo.Directory.FullName.ToString();  //프로그램 실행되고 있는데 path 가져오기
                string strManagerIniFile = "\\HTSManager.ini";
                string filePath = path + strManagerIniFile;   //ini 파일 경로 
                strUserInfoPath = filePath;
            }
            */
            SetPlatformPath();
            //GetUserInfo();
        }

        public void GetUserInfo()
        {
            if (_settingTarketType == SettingTarketType.PLATFORM)
                GetPlatformUserInfo();
            if (_settingTarketType == SettingTarketType.MANAGER)
                GetManagerUserInfo();
        }

        public void SetUserInfo()
        {
            if (_settingTarketType == SettingTarketType.PLATFORM)
                SetPlatformUserInfo();
            //if (_settingTarketType == SettingTarketType.MANAGER)
                //GetManagerUserInfo();
        }

        public void GetPlatformUserInfo()
        {
            switch (_platformType)
            {
                case PlatformType.MUG:
                case PlatformType.TX:
                    if (_UserInfoIni.GetIniValue("GENERAL", "SYSTEM_TYPE", "CLIENT").Equals("CLIENT"))
                    {
                        if (_UserInfoIni.GetIniValue("CLIENT", "COMM_TYPE_V2", "1").Equals("1"))
                        {
                            _ServerIP = "hts.wontrading.com";
                            _ConnectionServer = ConnectionServerType.OPERATION;
                        }
                        else
                        {
                            _ServerIP = _UserInfoIni.GetIniValue("CLIENT", "SERVER", "");
                            _ConnectionServer = ConnectionServerType.TEST;
                        }
                        _ServerType = ServerType.CUSTOMER;
                        if (_UserInfoIni.GetIniValue("CLIENT", "IS_SAVE_ID", "0") != "0")
                            _UserID = _UserInfoIni.GetIniValue("CLIENT", "USERID", "");
                        else
                            _UserID = "";
                        _IsMarketInquary = _UserInfoIni.GetIniValueToBoolean("INQUIRY", "IS_CHECK");
                    }
                    else
                    {
                        if (_UserInfoIni.GetIniValue("BRANCH", "SERVER", "staff.wm.com").Equals("staff.wm.com"))
                        {
                            _ServerIP = "staff.wm.com";
                            _ConnectionServer = ConnectionServerType.OPERATION;
                        }
                        else
                        {
                            _ServerIP = _UserInfoIni.GetIniValue("BRANCH", "SERVER", "");
                            _ConnectionServer = ConnectionServerType.TEST;
                        }
                        _ServerIP = _UserInfoIni.GetIniValue("BRANCH", "SERVER", "staff.wm.com");
                        if (_UserInfoIni.GetIniValue("BRANCH", "IS_SAVE_ID", "0") != "0")
                            _UserID = _UserInfoIni.GetIniValue("BRANCH", "USERID", "");
                        _ServerType = ServerType.EMPOLYEE;
                    }
                    break;
                case PlatformType.QTRADING:
                        _ServerIP = _UserInfoIni.GetIniValue("CLIENT", "SERVER", "newqtrade.wooriwm.com");
                        if(_ServerIP.Equals("newqtrade.wooriwm.com"))
                            _ConnectionServer = ConnectionServerType.OPERATION;
                        else
	                        _ConnectionServer = ConnectionServerType.TEST;
                        _ServerType = ServerType.CUSTOMER;
                        _UserID = _UserInfoIni.GetIniValue("CLIENT", "USERID", "");
                        _IsMarketInquary = _UserInfoIni.GetIniValueToBoolean("INQUIRY", "IS_CHECK");

                    break;
                case PlatformType.FORMDE:
                    _ServerType = ServerType.EMPOLYEE;
                    _ServerIP = _UserInfoIni.GetIniValue("COMM", "LATEST_SVR", "");
                    _UserID = _UserInfoIni.GetIniValue("COMM", "ID", "");
                    break;
                default:
                    break;
            }
        }

        public void SetPlatformUserInfo()
        {
            switch (_platformType)
            {
                case PlatformType.MUG:
                case PlatformType.TX:
                    if (_ServerType == ServerType.CUSTOMER)
                    {
                        _UserInfoIni.SetIniValue("GENERAL", "SYSTEM_TYPE", "CLIENT");
                        _UserInfoIni.SetIniValue("CLIENT", "USERID", _UserID.ToString());
                        _UserInfoIni.SetIniValueToBoolean("CLIENT", "IS_SAVE_ID", true);
                        _UserInfoIni.SetIniValueToBoolean("INQUIRY", "IS_CHECK", _IsMarketInquary);
                        if (_ConnectionServer == ConnectionServerType.OPERATION)
                            _UserInfoIni.SetIniValue("CLIENT", "COMM_TYPE_V2", "1");
                        else
                        {
                            _UserInfoIni.SetIniValue("CLIENT", "COMM_TYPE_V2", "2");
                            _UserInfoIni.SetIniValue("CLIENT", "SERVER", _ServerIP.ToString());
                        }
                    }
                    else
                    {
                        _UserInfoIni.SetIniValue("GENERAL", "SYSTEM_TYPE", "BRANCH");
                        _UserInfoIni.SetIniValue("BRANCH", "SERVER", _ServerIP.ToString());
                        _UserInfoIni.SetIniValue("BRANCH", "USERID", _UserID.ToString());
                        _UserInfoIni.SetIniValueToBoolean("BRANCH", "IS_SAVE_ID", true);
                    }                   
                    break;
                case PlatformType.QTRADING:
                    _UserInfoIni.SetIniValue("CLIENT", "SERVER", _ServerIP.ToString());
                    _UserInfoIni.SetIniValue("CLIENT", "USERID", _UserID.ToString());
                    _UserInfoIni.SetIniValueToBoolean("INQUIRY", "IS_CHECK", _IsMarketInquary);
                    break;
                case PlatformType.FORMDE:
                    _UserInfoIni.SetIniValue("COMM", "LASTES_SVR", _ServerIP.ToString());
                    _UserInfoIni.SetIniValue("COMM", "ID", _UserID.ToString());
                    break;
                default:
                    break;
            }
        }

        public void GetManagerUserInfo()
        {
            switch (_platformType)
            {
                case PlatformType.MUG:

                    break;
                case PlatformType.TX:

                    break;
                case PlatformType.QTRADING:

                    break;
                case PlatformType.FORMDE:

                    break;
                default:
                    break;
            }
        }

        public object Clone()
        {
            UserInfo userInfo = new UserInfo(_platformType, _settingTarketType);
            userInfo._ConnectionServer = this._ConnectionServer;
            userInfo._DownloadType = this._DownloadType;
            userInfo._IsCheckForgery = this._IsCheckForgery;
            userInfo._IsMarketInquary = this._IsMarketInquary;
            userInfo._platformType = this._platformType;
            userInfo._ServerIP = this._ServerIP;
            userInfo._ServerType = this._ServerType;
            userInfo._settingTarketType = this._settingTarketType;
            userInfo._UserCertPassword = this._UserCertPassword;
            userInfo._UserID = this._UserID;
            userInfo._UserInfoIni = this._UserInfoIni;
            userInfo._UserPassword = this._UserPassword;
            return userInfo;
        }
    }
}
